package com.kingkim.electromechanical.usercenter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.kingkim.electromechanical.commonutils.model.PageBean;
import com.kingkim.electromechanical.usercenter.model.po.Menu;
import com.kingkim.electromechanical.usercenter.service.IMenuService;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单controller层
 * @Auther Mr Zhang
 * */

@Controller
@RequestMapping("menu")
public class MenuController {

    /**
     * 日志
     * */
    private static final Logger log =Logger .getLogger(MenuController.class);
    //
    @Resource
    private IMenuService menuService;


    @RequestMapping("query")
    public @ResponseBody String queryForList(@ModelAttribute Menu menu,@RequestParam(value = "page",defaultValue="1") int page, @RequestParam(value = "limit",defaultValue="12")int limit){
        String JSONString = null;
        try {
            Map<String,Object> map = new HashMap();
            PageBean<Menu> p = menuService.QueryForList(page,limit,menu);
            map.put("data",p.getList());
            map.put("code",0);
            map.put("msg","success");
            map.put("count",p.getTotalCount());
            JSONString = JSON.toJSONString(map);
        }catch (Exception e){
            log.error("查询菜单列表异常："+e.getMessage());
        }
        return JSONString;
    }


    @RequestMapping("queryParent")
    public @ResponseBody String queryParentForList(){
        String JSONString = null;
        try {
            List list = menuService.QueryParentForList();
            JSONString = JSON.toJSONString(list);
        }catch (Exception e){
            log.error("查询父级菜单列表异常："+e.getMessage());
        }
        return JSONString;
    }


    @RequestMapping("delete")
    public @ResponseBody  String DeleteForList(@RequestParam("ids") String idJSON){
        String JSONString = null;
        try {
            List<String> ids = JSONArray.parseArray(idJSON, String.class);
            Integer flat = menuService.DeleteForList(ids);
            Map map =new  HashMap<String,Object>();
            map.put("result",flat);
            JSONString = JSON.toJSONString(map);
        }catch (Exception e){
            log.error("删除菜单信息异常:"+e.getMessage());
        }
        return JSONString;
    }


    @RequestMapping("save")
    public @ResponseBody String SaveOrModify(@ModelAttribute Menu menu){
        Map map = new HashMap<String, Object>();
        try {
            int flat = menuService.SaveOrModify(menu);
            map.put("result",flat);
        }catch (Exception e){
            log.error("保存菜单信息异常:"+e.getMessage());
        }
        return JSON.toJSONString(map);
    }
}
